package com.app.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.io.File;
import java.util.ArrayList;

import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import app.com.ModuleRole.R;

import com.app.Controller.ActivityController;
import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.AccountDTO;
import com.app.DTO.RoleDTO;
import com.app.Network.NetworkLayer;
import com.app.PhanQuyen.Quyen;

public  class frmPhuActivity extends Activity {
	public static Handler handler = new Handler();
	public static Runnable raShowMessage;
	public static Runnable raMoiVaoNhomNotif;
	public static Runnable raUpdateNguoiTrongNhom;
	public static Runnable raKhongDuocVaoNhom;
	public static Runnable raDaVaoNhom;
	public static Context baseContext;	
	public static String contentTitle,  contentText;
	AccountDTO account = null;
	ArrayList<Quyen> rule=null;
	static String loi = "";
	int port = 5033;
	Button btnSetVaiTro, btnXemVaiTro,btnTaoNhom,btnPhu,btnTruc,btnAn;
	TextView txtRole,txtDangNhap;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_phu_main);
		ActivityController.setPeerActivity(this);
		
		
		Button btnMoiVaoNhom = (Button) findViewById(R.id.btnMoiVaoNhom);
		btnXemVaiTro=(Button)findViewById(R.id.btnXemVaiTro);
		btnSetVaiTro=(Button)findViewById(R.id.btnSetVaiTro);
		btnTaoNhom=(Button)findViewById(R.id.btnTaoNhom);
		txtRole=(TextView)findViewById(R.id.txtRole);
		btnPhu=(Button)findViewById(R.id.btnPhu);
		btnTruc=(Button)findViewById(R.id.btnTruc);
		btnAn=(Button)findViewById(R.id.btnAn);
		txtDangNhap=(TextView)findViewById(R.id.txtDangNhap);
		btnMoiVaoNhom.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//ActivityController.showNotification("test thử xem", "test thử xem", "test thử xem");

			//	NetworkController.sendGroupInvite(new Address("192.168.1.77:"+port));
//				Intent i=new Intent(getBaseContext(),frmQuanLyRoleActivity.class);
//				startActivity(i);
				
				// network.account.getRule().get(0).setSend(false);
			}
		});
		btnXemVaiTro.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ArrayList<RoleDTO> roleList;//=ReadWriteFileController.readRole(ReadWriteFileController.MY_ROLE_PATH);
				roleList=NetworkController._peer.getAccount().getRoleList();
				String s="";
				for(RoleDTO role:roleList)
					s+=role.toString()+"; ";
				txtRole.setText(s);
			}
			
		});
		btnSetVaiTro.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ArrayList<RoleDTO> roleList=ReadWriteFileController.readRole(ReadWriteFileController.ROLE_DEFAULT_PATH);
				Log.d("Phu","path: "+ReadWriteFileController.ROLE_DEFAULT_PATH);
				Log.d("Phu","count: "+roleList.size());
				roleList.remove(0);
				roleList.remove(0);
				
				NetworkController.sendSetRole(new Address("192.168.1.88:"+port), roleList);
				
			}
		});
		btnTaoNhom.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(getBaseContext(), frmTaoNhom.class);
				startActivity(intent);
				
			}
		});
		btnPhu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			NetworkController._peer.account.set_Username("phu");
			NetworkController._peer.account.isThanhLapNhom=true;
			txtDangNhap.setText(NetworkController._peer.account.get_Username())	;
			}
		});
btnTruc.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			NetworkController._peer.account.set_Username("ptttruc");
			txtDangNhap.setText(NetworkController._peer.account.get_Username())	;
			}
		});
btnAn.setOnClickListener(new OnClickListener() {
	
	@Override
	public void onClick(View v) {
	NetworkController._peer.account.set_Username("hoaian");
	txtDangNhap.setText(NetworkController._peer.account.get_Username())	;
	}
});
		raShowMessage = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				showMessage(loi);
			}
		};
		raMoiVaoNhomNotif = new Runnable() {

			@Override
			public void run() {
				if(kiemTraVaoNhom()){
					displayNotification("Có người mời bạn vào nhóm!",contentText,contentTitle);
					//ActivityController.showNotification("Có người mời bạn vào nhóm", "asd", "asd", frmTaoNhom.this);
				}
			}
		};
		raUpdateNguoiTrongNhom=new Runnable() {
			
			@Override
			public void run() {
				capNhatNguoiTrongNhom(1);
				
			}
		};
		raKhongDuocVaoNhom=new Runnable() {
			
			@Override
			public void run() {
				show("Nhóm đã full, ko vào đc :))");
				
			}
		};
		raDaVaoNhom=new Runnable() {
			
			@Override
			public void run() {
				show("Da vao nhom roi do");
				
			}
		};
				
	}

	public void showMessage(String message) {
		
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setMessage(message);
		alert.setTitle("test");
		alert.show();
	}

	public static void show(String message) {
		loi = message;
		handler.post(raShowMessage);

	}

	@Override
	public void onStart() {
		super.onStart();
		if (NetworkController._peer == null) {
			String s="";
			AccountDTO account=new AccountDTO();
			account.set_Username("quocphu");
			ArrayList<RoleDTO> roleList=ReadWriteFileController.readRole(ReadWriteFileController.ROLE_DEFAULT_PATH);
			for(RoleDTO role:roleList){
				account.addRole(role);
				s+= role.toString()+"\n";
			}
			NetworkController.init("MES", port, account);
			
			int n=NetworkController._peer.account.getRule().size();
			for(int i=0;i<n;i++){
				Quyen q=NetworkController._peer.account.getRule().get(i);
				s+=i+q.getName()+q.getRead()+q.getWrite()+q.getSend()+q.getReceive()+"\n";
			}
			txtRole.setText( s);
		}
		
		String d="";
		for(int i=0;i<NetworkController._peer.account.getRule().size();i++){
			d+=NetworkController._peer.account.getRule().get(i).toString()+"\n";
		}
		TextView txtRoleMoi=(TextView)findViewById(R.id.txtRoleMoi);
		txtRoleMoi.setText(d);
		
		this.setTitle(NetworkController._peer.getAddressPeer());
		baseContext = getBaseContext();
	}

	public  void displayNotification(String tickerText,String contentTitle, String contentText) {

		Intent intent = new Intent(this, frmNotificationMoiVaoNhom.class);
		intent.putExtra("ID", 1);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				intent, 0);
		NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notif = new Notification(R.drawable.ic_launcher,
				tickerText, System.currentTimeMillis());
	
		notif.setLatestEventInfo(this, contentTitle, contentText,
				pendingIntent);

		notiManager.notify(1, notif);		
	}
	private void capNhatNguoiTrongNhom(int soNguoi){
		TextView txtSoNguoi=(TextView)findViewById(R.id.txtSoNguoiTrongNhom);
		txtSoNguoi.setText("Số người trong nhóm: "+soNguoi);
	}
	private boolean kiemTraVaoNhom(){
		return true;
	}
	
}
